package com.example.jiankang.Fragment;

import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;

import com.example.jiankang.HealthDatabaseHelper;
import com.example.jiankang.R;

public class jiluFragment extends Fragment {
    private HealthDatabaseHelper dbHelper;
    private EditText etWeight, etBloodPressure, etHeartRate, etGlucose;
    private TextView tvLastRecord;
    private Button btnSave;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jilu,container,false);

        dbHelper = new HealthDatabaseHelper(getActivity());
        etWeight = view.findViewById(R.id.et_weight);
        etBloodPressure = view.findViewById(R.id.et_blood_pressure);
        etHeartRate = view.findViewById(R.id.et_heart_rate);
        etGlucose = view.findViewById(R.id.et_glucose);
        btnSave = view.findViewById(R.id.btn_save);
        tvLastRecord = view.findViewById(R.id.tv_last_record);

        //显示最新记录
        showLatestRecord();

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        return view;
    }

    private void showLatestRecord() {
        Cursor cursor = dbHelper.getLatestRecord();
        if (cursor != null && cursor.moveToFirst()) {
            double weight = cursor.getDouble(cursor.getColumnIndexOrThrow(HealthDatabaseHelper.COLUMN_WEIGHT));
            String bp = cursor.getString(cursor.getColumnIndexOrThrow(HealthDatabaseHelper.COLUMN_BLOOD_PRESSURE));
            int hr = cursor.getInt(cursor.getColumnIndexOrThrow(HealthDatabaseHelper.COLUMN_HEART_RATE));
            double glucose = cursor.getDouble(cursor.getColumnIndexOrThrow(HealthDatabaseHelper.COLUMN_GLUCOSE));

            String recordText = "上次记录显示区：\n"
                    + "体重: " + weight + "kg\n"
                    + "血压: " + bp + "\n"
                    + "心率: " + hr + "次/分钟\n"
                    + "血糖: " + glucose + "mmol/L";

            tvLastRecord.setText(recordText);
            cursor.close();
        } else {
            tvLastRecord.setText("暂无历史记录");
        }
    }

    private void saveHealthData() {
        try {
            double weight = Double.parseDouble(etWeight.getText().toString());
            String bloodPressure = etBloodPressure.getText().toString();
            int heartRate = Integer.parseInt(etHeartRate.getText().toString());
            double glucose = Double.parseDouble(etGlucose.getText().toString());

            long result = dbHelper.insertRecord(weight, bloodPressure, heartRate, glucose);

            if (result != -1) {
                Toast.makeText(getActivity(), "记录保存成功", Toast.LENGTH_SHORT).show();
                showLatestRecord(); //刷新显示
                clearInputs();
            }
        } catch (NumberFormatException e) {
            Toast.makeText(getActivity(), "请检查输入格式", Toast.LENGTH_SHORT).show();
        }
    }

    private void clearInputs() {
        etWeight.setText("");
        etBloodPressure.setText("");
        etHeartRate.setText("");
        etGlucose.setText("");
    }

    @Override
    public void onDestroy() {
        dbHelper.close();
        super.onDestroy();
    }
}